﻿package    

{   
	import flash.text.*;
	import com.adobe.serialization.json.JSON;
	import com.adobe.crypto.MD5;
	import strhelp.StringHelper;
	import flash.events.*;
	import flash.display.Sprite;   
	import flash.errors.*;   
	import flash.external.ExternalInterface;
	import flash.utils.Timer; 
	import flash.events.TimerEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	public class SocketPhp extends Sprite   
	{ 
		private var myTimer:Timer;				
		private var strHelper:StringHelper = new StringHelper();				
		private var loader:URLLoader;
		private var awdstart = false;
		
		public function SocketPhp()   
		{    	
			loginBt.buttonMode=true;
			loginBt.addEventListener(MouseEvent.CLICK,loginHandler); 
			
			
			loginBt2.buttonMode=true;
			loginBt2.addEventListener(MouseEvent.CLICK,stopawdHandler);
			
			loginErrClose.buttonMode=true;
			loginErrClose.addEventListener(MouseEvent.CLICK,closeErrHandler); 
			loginBt2.visible =false;

 			loginErr.visible =false;
			loginErrClose.visible=false;
			errInfo.visible=false;
			hideAllAwardLight();
			initAwd();
		}
		public function hideAllAwardLight()
		{
			awl1.visible =false;
			awl2.visible =false;
			awl3.visible =false;
			awl4.visible =false;
			awl5.visible =false;
			awl6.visible =false;
			awl7.visible =false;
			awl8.visible =false;
		}
		public function initAwd()
		{
			loader = new URLLoader(new URLRequest("http://www.zuizhuan.com/user/lottery/do/awardlist?ajax=1"));
			loader.addEventListener(Event.COMPLETE, completeAwdlist);
		}
		public function showAwardLight(num:String):void
		{
			hideAllAwardLight();
			if(num=='1'){
				awl1.visible=true;
			}
			if(num=='2'){
				awl2.visible=true;
			}
			if(num=='3'){
				awl3.visible=true;
			}
			if(num=='4'){
				awl4.visible=true;
			}
			if(num=='5'){
				awl5.visible=true;
			}
			if(num=='6'){
				awl6.visible=true;
			}
			if(num=='7'){
				awl7.visible=true;
			}
			if(num=='8'){
				awl8.visible=true;
			}
		}
		
		//698d51a19d8a121ce581499d7b701668
		private function loginHandler(event:MouseEvent):void
		{
			loginBt.buttonMode=false;
			loginBt.visible =false;						
			loginBt.buttonMode=true;
			loginBt2.visible =true;
			hideAllAwardLight();
			loginBg.gotoAndPlay(2);	
			
			
		}
		private function stopawdHandler(event:MouseEvent):void
		{
			if(awdstart==false){
				awdstart=true;
				hideAllAwardLight();
				loader = new URLLoader(new URLRequest("http://www.zuizhuan.com/user/lottery/do/award?ajax=1"));
				loader.addEventListener(Event.COMPLETE, completeLoinHandler);							
			}
		}
		
		public function showErrMsg(showStr:String):void
		{
			loginErr.visible =true;
			loginErrClose.visible=true;
			errInfo.visible=true;
			errInfo.text=showStr;
		}
		private function closeErrHandler(event:MouseEvent):void
		{
			loginErr.visible =false;
			loginErrClose.visible=false;
			errInfo.visible=false;
		}
		private function HideErrMsg():void
		{
			loginErr.visible =false;
			loginErrClose.visible=false;
			errInfo.visible=false;
		}
		private function completeAwdlist(event:Event):void
		{
			var jsondata:Object = JSON.decode(loader.data );
			if(jsondata.result==0){
				showErrMsg(jsondata.msg);
				intbtn();
				
			}else{
				
				aw1.text = jsondata.list[1]
				aw2.text = jsondata.list[2]
				aw3.text = jsondata.list[3]
				aw4.text = jsondata.list[4]
				aw5.text = jsondata.list[5]
				aw6.text = jsondata.list[6]
				aw7.text = jsondata.list[7]
				aw8.text = jsondata.list[8]
				
			}
		}
		
		private function completeLoinHandler(event:Event):void
		{
			loginBg.stop();
			loginBg.gotoAndPlay(1);
			loginBg.stop();
			
			
			var jsondata:Object = JSON.decode(loader.data );
			if(jsondata.result==0){
				showErrMsg(jsondata.msg);
				intbtn()
				
			}else{
				showAwardLight(jsondata.num);
				myTimer = new Timer(500, 0);
            	myTimer.addEventListener(TimerEvent.TIMER, waitetime);
            	myTimer.start();
			}
			
		}
		public function waitetime(event:Event):void
		{			
			myTimer.stop();
			var jsondata:Object = JSON.decode(loader.data );
			showErrMsg(jsondata.msg2);
			//awdlist.appendText(jsondata.msg+"\n");
			ExternalInterface.call("loadswf",jsondata.msg);
			//trace(jsonArray);
			intbtn();
			
		}
		private function intbtn():void
		{
			loginBt.buttonMode=true;
			loginBt.visible =true;
			loginBt2.visible =false;
			awdstart = false;
		}
		private function bindDomain() : Boolean
       	{
        	var _loc_1:* = loaderInfo.url.toString().toLocaleLowerCase();
            var _loc_2:Boolean = false;
            var _loc_3:Array = ["file:///", "http://localhost", "zuizhuan.com", "http://192.168"];
            var _loc_4:uint = 0;
            while ( _loc_4 < _loc_3.length )
            {                
            	if ( _loc_1.indexOf(_loc_3[_loc_4].toString()) >= 0 )
            	{
               		_loc_2 = true;
                }
                _loc_4 = _loc_4 + 1;
            }
			
            return _loc_2;
        }		
	}
}
